// Copyright (c) 2011 The Kiwii Authors. All rights reserved.
// Use of this source code is governed by the New BSD License that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

#ifndef KIWI_LOGGER_INCLUDED
#define KIWI_LOGGER_INCLUDED

#include <string>
#include <stdarg.h>
#include <stdio.h>

namespace kiwii {
  namespace comm {

    class Logger {
    public:
      typedef enum {TRACE, DEBUG, INFO, WARNING, ERROR, FATAL} Level;

      static Logger& get() {
	static Logger logger_;
	return logger_;
      }

      Level getLevel() const {
	return level_;
      }

      void setLevel(Level l) {
	level_ = l;
      }

      void fatal(char const* format, ...) {
	if (level_ > FATAL) return;
	
	va_list ap;
	va_start(ap, format);
	log(FATAL, format, ap);
	va_end(ap);
      }

      void error(char const* format, ...) {
	if (level_ > ERROR) return;

	va_list ap;
	va_start(ap, format);
	log(ERROR, format, ap);
	va_end(ap);
      }

      void warning(char const* format, ...) {
	if (level_ > WARNING) return;

	va_list ap;
	va_start(ap, format);
	log(WARNING, format, ap);
	va_end(ap);
      }

      void info(char const* format, ...) {
	if (level_ > INFO) return;

	va_list ap;
	va_start(ap, format);
	log(INFO, format, ap);
	va_end(ap);
      }

      void debug(char const* format, ...) {
	if (level_ > DEBUG) return;

	va_list ap;
	va_start(ap, format);
	log(DEBUG, format, ap);
	va_end(ap);
      }

      void trace(char const* format, ...) {
	if (level_ > TRACE) return;

	va_list ap;
	va_start(ap, format);
	log(TRACE, format, ap);
	va_end(ap);
      }

    private:
      Logger();
      virtual ~Logger() {}

      void log(Level level, char const* fmt, va_list ap);

      Level level_;  
      FILE* logfile_;
    };


  }
}

#endif
